

package com.zmks.jkgj.xuexiao.controller;



import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.zmks.jkgj.core.controller.ABaseController;
import com.zmks.jkgj.core.resp.Resp;
import com.zmks.jkgj.core.util.IdcardValidator;
import com.zmks.jkgj.xuexiao.SessionManager;
import com.zmks.jkgj.xuexiao.resp.WebResp;
import com.zmks.jkgj.xuexiao.service.ICrctObjClassService;
import com.zmks.jkgj.xuexiao.service.ICrctObjParentService;
import com.zmks.jkgj.xuexiao.service.ICrctObjStudentService;

import com.zmks.jkgj.xuexiao.vo.CrctObjClassVO;
import com.zmks.jkgj.xuexiao.vo.CrctObjParentVO;
import com.zmks.jkgj.xuexiao.vo.CrctObjStudentVO;
import com.zmks.jkgj.xuexiao.vo.CrctObjTeacherVO;
/**
 * 学生操作
 * 
 * @author jiangyaning
 * 
 */
@Controller
public class CrctObjStudentController extends ABaseController {
	@Autowired
	
	private ICrctObjStudentService crctObjStudentService;	
	   @Autowired
		
		private ICrctObjClassService crctObjClassService;	
	@Autowired
	
	private ICrctObjParentService crctObjParentService;	

	

	 //查询学生
	@RequestMapping("/get_crctObjStudent_list")
	@ResponseBody
	public Resp get_crctObjStudent_list( Integer pageIndex,
			Integer pageSize,HttpServletRequest request) {
		WebResp 	webResp = new WebResp();
		CrctObjStudentVO vs=	new CrctObjStudentVO();
	   String  school_oid = SessionManager.getsession(request).getSchool_oid();
		  vs.setSchool_oid(Long.parseLong(school_oid));
			webResp.setData(crctObjStudentService.getList(vs));
			webResp.setTotalCount(crctObjStudentService.getCount(vs));
	
		return webResp;
	}
	 //增加学生
	@RequestMapping("/save_CrctObjStudent")
	@ResponseBody
	public Resp save_CrctObjStudent( 
			String name,
			String account_oid,
			String birthday,
			String class_oid,
			String first_contact_oid,
			 
			String student_id,
			String sex,
	 HttpServletRequest request) {
		WebResp 	webResp = new WebResp();
		CrctObjStudentVO crctObjStudentVO=new  CrctObjStudentVO();
		crctObjStudentVO.setName(name);
		crctObjStudentVO.setStudent_id(Long.parseLong(student_id));
		crctObjStudentVO.setAccount_oid(Long.parseLong(account_oid));
		crctObjStudentVO.setSex(sex);
		 crctObjStudentVO.setBirthday(new Date());
		crctObjStudentVO.setClass_oid(Long.parseLong(class_oid));
		crctObjStudentVO.setCreate_date(new Date());
		crctObjStudentVO.setFirst_contact_oid(Long.parseLong(first_contact_oid));
	   String  school_oid = SessionManager.getsession(request).getSchool_oid();
		crctObjStudentVO.setSchool_oid(Long.parseLong(school_oid));
		crctObjStudentVO.setModify_date(new Date());
	    crctObjStudentService.save(crctObjStudentVO);
	    //增加家长
	    //
		return webResp;
	}
	 //删除学生
	@RequestMapping("/remove_CrctObjStudent")
	@ResponseBody
	public Resp remove_CrctObjStudent( Long id,HttpServletRequest request) {
		WebResp 	webResp = new WebResp();
		CrctObjStudentVO crctObjStudentVO=new  CrctObjStudentVO();
	    crctObjStudentService.remove(id);
		return webResp;
	}
	
	 //增加学生
	@RequestMapping("/save_CrctObjStudent_parent")
	@ResponseBody
	public Resp save_CrctObjStudent_parent( 
			String student_id,
			String name,
            String card_id,
			String birthday,
			String sex,
			String mobile,
			String class_oid,
			String jiazhang_name,
			String jiazhang_card,
			String jiazhang_birthday,
			String jiazhang_tel,
			String jiazhang_work,
			String jiazhang_relation,
			String jiazhang_mail,
		  HttpServletRequest request) {
		WebResp 	webResp = new WebResp();
		try{
	
	SimpleDateFormat sdf =   new SimpleDateFormat("yyyy-MM-dd" );
	 if(student_id.equals("")||name.equals("")||jiazhang_name.equals(""))
	 {
			webResp.setRet(1);
			webResp.setErrcode(0);
			webResp.setMsg("学生姓名和家长姓名不能为空！");
			return webResp;
	 }
		
	try{
			
			
			sdf.parse(birthday);
			} catch (Exception e) {
				e.printStackTrace();
				webResp.setRet(1);
				webResp.setErrcode(0);
				webResp.setMsg("生日格式错误！");
				return webResp;
			}
			try{
			    sdf.parse(birthday);
				} catch (Exception e) {
					e.printStackTrace();
					webResp.setRet(1);
					webResp.setErrcode(0);
					webResp.setMsg("家长生日格式错误！");
					return webResp;
				}
			
			IdcardValidator iv = new IdcardValidator();
			if(!iv.isValidatedAllIdcard(card_id)){
				webResp.setRet(1);
				webResp.setErrcode(0);
				webResp.setMsg("学生身份证填写错误！");
				return webResp;
			}
		
	
			if(!iv.isValidatedAllIdcard(jiazhang_card)){
				webResp.setRet(1);
				webResp.setErrcode(0);
				webResp.setMsg("家长身份证填写错误！");
				return webResp;
			}
			//学校
		String  school_oid = SessionManager.getsession(request).getSchool_oid();
		CrctObjStudentVO crctObjStudentVO=new  CrctObjStudentVO();
		crctObjStudentVO.setStudent_id(Long.parseLong(student_id));
		crctObjStudentVO.setBirthday(sdf.parse(birthday));
		crctObjStudentVO.setName(name);
		crctObjStudentVO.setCard_id(card_id);
		crctObjStudentVO.setMobile(mobile);
	    crctObjStudentVO.setSex(sex);
		crctObjStudentVO.setClass_oid(Long.parseLong(class_oid));
		crctObjStudentVO.setCreate_date(new Date());
	   crctObjStudentVO.setSchool_oid(Long.parseLong(school_oid));
		crctObjStudentVO.setCreate_date(new Date());
		CrctObjStudentVO crctObjStudentVO2=  crctObjStudentService.save(crctObjStudentVO);
	    //增加家长
	    CrctObjParentVO crctObjParentVO=new  CrctObjParentVO();
	     crctObjParentVO.setStudent_oid(crctObjStudentVO2.getOid().toString());
		   crctObjParentVO.setName(jiazhang_name);
		   crctObjParentVO.setSex(sex);
		   crctObjParentVO.setBirthday(sdf.parse(jiazhang_birthday));
		   crctObjParentVO.setCard_id(jiazhang_card);
		   crctObjParentVO.setMobile(jiazhang_tel);
		   crctObjParentVO.setWork(jiazhang_work);
		   crctObjParentVO.setRelation(jiazhang_relation);
		   crctObjParentVO.setEmail(jiazhang_mail);
		   crctObjParentVO.setCreate_date(new Date());
		   crctObjParentService.save(crctObjParentVO);
		} catch (Exception e) {
			e.printStackTrace();
			webResp.setRet(1);
			webResp.setErrcode(0);
			webResp.setMsg("获取失败！");
			return webResp;
		}
		return webResp;
	}
	
	
	 //修改学生
	@RequestMapping("/update_CrctObjStudent_parent")
	@ResponseBody
	public Resp update_CrctObjStudent_parent(
			Long oid ,
			String student_id,
			String name,
            String card_id,
			String birthday,
			String sex,
			String mobile,
			String class_oid,
			String jiazhang_name,
			String jiazhang_card,
			String jiazhang_birthday,
			String jiazhang_tel,
			String jiazhang_work,
			String jiazhang_relation,
			String jiazhang_mail,
		  HttpServletRequest request) {
		WebResp 	webResp = new WebResp();
		try{
	
	SimpleDateFormat sdf =   new SimpleDateFormat("yyyy-MM-dd" );
	 if(student_id.equals("")||name.equals("")||jiazhang_name.equals(""))
	 {
			webResp.setRet(1);
			webResp.setErrcode(0);
			webResp.setMsg("学生姓名和家长姓名不能为空！");
			return webResp;
	 }
		
	try{
			
			
			sdf.parse(birthday);
			} catch (Exception e) {
				e.printStackTrace();
				webResp.setRet(1);
				webResp.setErrcode(0);
				webResp.setMsg("生日格式错误！");
				return webResp;
			}
			try{
			    sdf.parse(birthday);
				} catch (Exception e) {
					e.printStackTrace();
					webResp.setRet(1);
					webResp.setErrcode(0);
					webResp.setMsg("家长生日格式错误！");
					return webResp;
				}
			
			IdcardValidator iv = new IdcardValidator();
			if(!iv.isValidatedAllIdcard(card_id)){
				webResp.setRet(1);
				webResp.setErrcode(0);
				webResp.setMsg("学生身份证填写错误！");
				return webResp;
			}
		
	
			if(!iv.isValidatedAllIdcard(jiazhang_card)){
				webResp.setRet(1);
				webResp.setErrcode(0);
				webResp.setMsg("家长身份证填写错误！");
				return webResp;
			}
			//学校
		String  school_oid = SessionManager.getsession(request).getSchool_oid();
		
		CrctObjStudentVO crctObjStudentVO_s=new  CrctObjStudentVO();
		crctObjStudentVO_s.setOid(oid);
		CrctObjStudentVO crctObjStudentVO=crctObjStudentService.getUniqueResult(crctObjStudentVO_s);
		
	//	CrctObjStudentVO crctObjStudentVO=new  CrctObjStudentVO();
		crctObjStudentVO.setStudent_id(Long.parseLong(student_id));
		crctObjStudentVO.setBirthday(sdf.parse(birthday));
		crctObjStudentVO.setName(name);
		crctObjStudentVO.setCard_id(card_id);
		crctObjStudentVO.setMobile(mobile);
	    crctObjStudentVO.setSex(sex);
		crctObjStudentVO.setClass_oid(Long.parseLong(class_oid));
		crctObjStudentVO.setCreate_date(new Date());
	   crctObjStudentVO.setSchool_oid(Long.parseLong(school_oid));
		crctObjStudentVO.setCreate_date(new Date());
		CrctObjStudentVO crctObjStudentVO2=  crctObjStudentService.save(crctObjStudentVO);
	    //增加家长
	    CrctObjParentVO crctObjParentVO_s=new  CrctObjParentVO();
	    crctObjParentVO_s.setStudent_oid(oid.toString());
		List<CrctObjParentVO> crctObjParentVO_list=crctObjParentService.getList(crctObjParentVO_s);
		CrctObjParentVO crctObjParentVO=null;
		if(crctObjParentVO_list.size()>=1)
		{
			crctObjParentVO=crctObjParentVO_list.get(0);
		}else
		{
			crctObjParentVO=new CrctObjParentVO();	
		}
	      crctObjParentVO.setStudent_oid(crctObjStudentVO2.getOid().toString());
		   crctObjParentVO.setName(jiazhang_name);
		   crctObjParentVO.setSex(sex);
		   crctObjParentVO.setBirthday(sdf.parse(jiazhang_birthday));
		   crctObjParentVO.setCard_id(jiazhang_card);
		   crctObjParentVO.setMobile(jiazhang_tel);
		   crctObjParentVO.setWork(jiazhang_work);
		   crctObjParentVO.setRelation(jiazhang_relation);
		   crctObjParentVO.setEmail(jiazhang_mail);
		   crctObjParentVO.setCreate_date(new Date());
		   crctObjParentService.save(crctObjParentVO);
		} catch (Exception e) {
			e.printStackTrace();
			webResp.setRet(1);
			webResp.setErrcode(0);
			webResp.setMsg("获取失败！");
			return webResp;
		}
		return webResp;
	}
	
	 //查询
	@RequestMapping("/get_CrctObjStudent_parent_one")
	@ResponseBody
	public Resp get_CrctObjStudent_parent_one( Long oid,HttpServletRequest request) {
        WebResp webResp = new WebResp();
        //学生
        CrctObjStudentVO crctObjStudentVO_s=new  CrctObjStudentVO();
		crctObjStudentVO_s.setOid(oid);
		CrctObjStudentVO crctObjStudentVO=crctObjStudentService.getUniqueResult(crctObjStudentVO_s);
		//家长
		 CrctObjParentVO crctObjParentVO_s=new  CrctObjParentVO();
		    crctObjParentVO_s.setStudent_oid(oid.toString());
			List<CrctObjParentVO> crctObjParentVO_list=crctObjParentService.getList(crctObjParentVO_s);
			CrctObjParentVO crctObjParentVO=null;
			if(crctObjParentVO_list.size()>=1)
			{
				crctObjParentVO=crctObjParentVO_list.get(0);
			}
			 HashMap data=new HashMap();
			 data.put("xuesheng", crctObjStudentVO);
			CrctObjClassVO crctObjClassVO=new  CrctObjClassVO();
			crctObjClassVO.setOid(crctObjStudentVO.getClass_oid());
				CrctObjClassVO v=crctObjClassService.getUniqueResult(crctObjClassVO);
			    HashMap  map=new HashMap();
			    map.put("id", v.getOid());
			    map.put("name", v.getName());
			    data.put("banji", map);
			 data.put("jiazhang", crctObjParentVO);
			webResp.setData(data);
			return webResp;
	}

}
